package by.semerikov.servlets;

import java.io.IOException; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 


import by.semerikov.controlers.ControlFactory;
import by.semerikov.controlers.Controller;
import by.semerikov.exceptions.ApplicationException;
import by.semerikov.exceptions.ControllerNotFoundException;

/**
 * Servlet implementation class TransportSystemServlet
 */
public class TransportSystemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TransportSystemServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			/*HttpSession session = request.getSession(true);
		    response.setContentType("text/html");
		    if(session.getAttribute("id")== null)
		    {
		    	session.setAttribute("id",request.getParameter("id"));
		    }
		    PrintWriter out = response.getWriter();
		    out.println("<html>");
	        out.println("<head>");
	        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
	        out.println("<title>" +session.getAttribute("id")+"</title>");
	        out.println("</head>");
	        out.println("<body>");
	        out.println(session.getAttribute("id")+"</br>");
	        out.println(request.getParameter("id")+"</br>");
	        out.println("</body>");*/
		    try {
		Command command = new WebCommand(request);
		String role = "GUEST";
		Controller controller = ControlFactory.getFactory(role).getController(command.nameCommand);
		Result result = controller.execute(command);
		request.setAttribute("Result", result);
		//later I should deside to what jsp I can forward response
		} 
		 catch (ControllerNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ApplicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
